//判断是否为空
export function isEmpty(...strs) {
  return strs.some(val => {
    if (val.trim().length == 0) return true
  })
}

//转换时间的格式
export function changeTime(val) {
  //当前的时间
  let todayDate = new Date();
  let todayYear = todayDate.getFullYear(); // 获取完整的年份(4位,1970)
  let todayMonth = todayDate.getMonth() + 1; // 获取月份(0-11,0代表1月,用的时候记得加上1)
  let todayDay = todayDate.getDate(); // 获取日(1-31)

  //发布朋友圈的时间
  let date = new Date(val);
  let year = date.getFullYear(); // 获取完整的年份(4位,1970)
  let month = date.getMonth() + 1; // 获取月份(0-11,0代表1月,用的时候记得加上1)
  let day = date.getDate(); // 获取日(1-31)
  let hours = date.getHours(); // 获取小时数(0-23)
  let minutes = date.getMinutes(); // 获取分钟数(0-59)

  if(hours < 9) hours = "0" + hours
  if(minutes < 9) minutes = "0" + minutes

  if (todayYear == year) {
    if (todayDay == day && month == todayMonth) {
      return "今天" + hours + ":" + minutes;
    } else if (todayDay - 1 == day && month == todayMonth) {
      return "昨天" + hours + ":" + minutes;
    } else if (todayDay - 2 == day && month == todayMonth) {
      return "前天" + hours + ":" + minutes;
    } else {
      return month + "月" + day + "日" + hours + ":" + minutes;
    }
  } else {
    return year + "年" + month + "月" + day + "日" + hours + ":" + minutes;
  }
}

//将时间转换为周几
export function getWeekDay(days) {
  if (days == 7) days = 0
  switch (days) {
    case 1:
      days = '周一';
      break;
    case 2:
      days = '周二';
      break;
    case 3:
      days = '周三';
      break;
    case 4:
      days = '周四';
      break;
    case 5:
      days = '周五';
      break;
    case 6:
      days = '周六';
      break;
    case 0:
      days = '周日';
      break;
  }
  return days
}

//推算接下来一周的时间(部分只包括 月日)
export function figureOutWeek(today) {
  var dateArr = []
  for (var i = 0; i < 7; i++) {
    var newDate = new Date(today.getTime() + i * 1000 * 60 * 60 * 24)
    var month = (parseInt(newDate.getMonth()) + 1) > 9 ? (parseInt(newDate.getMonth()) + 1) : + (parseInt(newDate.getMonth()) + 1)
    var day = (newDate.getDate()) > 9 ? newDate.getDate() : + newDate.getDate()
    var fullDate = `${month}.${day}`
    dateArr.push(fullDate)
  }
  return dateArr
}

//推算接下来一周的时间(部分只包括 年月日)
export function wholeFigureOutWeek(today) {
  var dateArr = []
  for (var i = 0; i < 7; i++) {
    var newDate = new Date(today.getTime() + i * 1000 * 60 * 60 * 24)
    var year = newDate.getFullYear()
    var month = (parseInt(newDate.getMonth()) + 1) > 9 ? (parseInt(newDate.getMonth()) + 1) :  (parseInt(newDate.getMonth()) + 1)
    var day = (newDate.getDate()) > 9 ? newDate.getDate() :  newDate.getDate()
    var fullDate = `${year}-${month}-${day}`
    dateArr.push(fullDate)
  }
  return dateArr
}

//获取城市的定位
export const getLocation=(callback)=>{
  // var map = new AMap.Map(box, {
  //     resizeEnable: true,
  //     center: [116.397428, 39.90923],
  //     zoom: 13
  // });
  //实例化城市查询类
  var citysearch = new AMap.CitySearch();
  //自动获取用户IP，返回当前城市
  citysearch.getLocalCity(function(status, result) {
      if (status === 'complete' && result.info === 'OK') {
          if (result && result.city && result.bounds) {
              var cityinfo = result.city;
              // var citybounds = result.bounds;
              callback(cityinfo)
              //地图显示当前城市
              // map.setBounds(citybounds);
          }
      } else {
          callback(result.info)
      }
  });
}

//图片转码
export function getUrlBase642(url, ext, callback) {
  var canvas = document.createElement("canvas"); //创建canvas DOM元素
  var ctx = canvas.getContext("2d");
  var img = new Image;
  img.crossOrigin = 'Anonymous';
  img.src = url;
  img.onload = function () {
    canvas.height = 85; //指定画板的高度,自定义
    canvas.width = 85; //指定画板的宽度，自定义
    ctx.drawImage(img, 0, 0, 85, 85); //参数可自定义
    var dataURL = canvas.toDataURL("image/" + ext);
    callback.call(this, dataURL); //回掉函数获取Base64编码
    canvas = null;
  };
}